ForceRequiredAddOn("System_BlocklandGlass");

function registerPrefs() {
    if(isFunction(registerPreferenceAddon)) {
        RegisterGlassPrefs();
    } else {
        ExtendDefaultPrefValues();
    }

    DeleteOutdatedPrefs();
}

function RegisterGlassPrefs()
{
    registerPreferenceAddon("Gamemode_Stargate", "Stargate", "star");

    new ScriptObject(Preference) {
        className      = "StargatePrefs";

        addon          = "Gamemode_Stargate";
        category       = "Generation";
        title          = "RNG seed for hidden systems";

        type           = "num";
        params         = "671 100000000000000 0";

        variable       = "$Pref::Server::Stargate::RNGSeed";

        defaultValue   = 1563980479;

        hostOnly       = true;

        loadNow        = true;

        requireRestart = true;

        hostOnly       = true;
    };

    new ScriptObject(Preference) {
        className      = "StargatePrefs";

        addon          = "Gamemode_Stargate";
        category       = "Generation";
        title          = "Maximum hidden addresses";

        type           = "num";
        params         = "1 1024 0";

        variable       = "$Pref::Server::Stargate::MaxHiddenAddresses";

        defaultValue   = 816;

        loadNow        = true;

        requireRestart = true;

        hostOnly       = true;
    };

    new ScriptObject(Preference) {
        className      = "StargatePrefs";

        addon          = "Gamemode_Stargate";
        category       = "Mechanics";
        title          = "Planet chat range bypass?";

        type           = "bool";

        variable       = "$Pref::Server::Stargate::ChatBypass";

        defaultValue   = false;

        loadNow        = true;

        hostOnly       = true;

        requireRestart = false;
    };

    new ScriptObject(Preference) {
        className      = "StargatePrefs";

        addon          = "Gamemode_Stargate";
        category       = "Mechanics";
        title          = "Planet chat range bypass rank";

        type           = "dropdown";
        params         = "Host 3 Super_Admin 2 Admin 1";

        variable       = "$Pref::Server::Stargate::ChatBypassRank";

        defaultValue   = "Super_Admin 2";

        loadNow        = true;

        hostOnly       = true;
        
        requireRestart = false;
    };

    new ScriptObject(Preference) {
        className      = "StargatePrefs";

        addon          = "Gamemode_Stargate";
        category       = "Mechanics";
        title          = "World border bypass?";

        type           = "bool";

        variable       = "$Pref::Server::Stargate::BypassBorder";

        defaultValue   = false;

        loadNow        = true;

        hostOnly       = true;

        requireRestart = false;
    };

    new ScriptObject(Preference) {
        className      = "StargatePrefs";

        addon          = "Gamemode_Stargate";
        category       = "Mechanics";
        title          = "World border bypass rank";

        type           = "dropdown";
        params         = "Host 3 Super_Admin 2 Admin 1";

        variable       = "$Pref::Server::Stargate::BypassBorderRank";

        defaultValue   = "Super_Admin 2";

        loadNow        = true;

        hostOnly       = true;

        requireRestart = false;
    };

    new ScriptObject(Preference) {
        className      = "StargatePrefs";

        addon          = "Gamemode_Stargate";
        category       = "Mechanics";
        title          = "Duplicate check? (disable for compatibility with old saves)";

        type           = "bool";

        variable       = "$Pref::Server::Stargate::DuplicateCheck";

        defaultValue   = true;

        loadNow        = true;

        hostOnly       = true;

        requireRestart = true;
    };

    new ScriptObject(Preference) {
        className      = "StargatePrefs";

        addon          = "Gamemode_Stargate";
        category       = "Mechanics";
        title          = "Enable custom environments?";

        type           = "bool";

        variable       = "$Pref::Server::Stargate::EnvironMode";

        defaultValue   = false;

        loadNow        = true;

        hostOnly       = true;

        requireRestart = false;
    };

    new ScriptObject(Preference) {
        className      = "StargatePrefs";

        addon          = "Gamemode_Stargate";
        category       = "Commands";
        title          = "Enable /whatsmyplanet command";

        type           = "bool";

        variable       = "$Pref::Server::Stargate::WhatsMyPlanetCmd";

        defaultValue   = false;

        hostOnly       = true;

        loadNow        = true;

        requireRestart = false;
    };

    new ScriptObject(Preference) {
        className      = "StargatePrefs";

        addon          = "Gamemode_Stargate";
        category       = "Commands";
        title          = "/whatsmyplanet command rank";

        type           = "dropdown";
        params         = "Host 3 Super_Admin 2 Admin 1";

        variable       = "$Pref::Server::Stargate::WhatsMyPlanetCmdRank";

        defaultValue   = "Super_Admin 2";

        loadNow        = true;

        hostOnly       = true;

        requireRestart = false;
    };

    new ScriptObject(Preference) {
        className      = "StargatePrefs";

        addon          = "Gamemode_Stargate";
        category       = "General";
        title          = "Reset preferences to default";

        type           = "bool";

        variable       = "$Stargate::RestoreDefaultPrefs";

        updateCallback = "RestoreDefaultPrefs";

        defaultValue   = false;

        hostOnly       = true;

        loadNow        = true;
    };
}

function RestoreDefaultPrefs()
{
	if($Stargate::RestoreDefaultPrefs)
		ApplyDefaultPrefValues();
}

function ExtendDefaultPrefValues()
{
	echo("[" @ getWord(getDateTime(), 1) @ "] [Stargate] Extending default pref values");

	if($Pref::Server::Stargate::MaxHiddenAddresses $= "") $Pref::Server::Stargate::MaxHiddenAddresses = 816;
    if($Pref::Server::Stargate::RNGSeed            $= "") $Pref::Server::Stargate::RNGSeed = 1563980479;
    if($Pref::Server::Stargate::ChatBypass         $= "") $Pref::Server::Stargate::ChatBypass = false;
    if($Pref::Server::Stargate::ChatBypassRank     $= "") $Pref::Server::Stargate::ChatBypassRank = 2;
    if($Pref::Server::Stargate::BypassBorder       $= "") $Pref::Server::Stargate::BypassBorder = false;
    if($Pref::Server::Stargate::BypassBorder       $= "") $Pref::Server::Stargate::BypassBorderRank = 2;
    if($Pref::Server::Stargate::WhatsMyPlanetCmd   $= "") $Pref::Server::Stargate::WhatsMyPlanetCmd = false;
    if($Pref::Server::Stargate::WhatsMyPlanetCmdRank $= "") $Pref::Server::Stargate::WhatsMyPlanetCmdRank = 2;
    if($Pref::Server::Stargate::DuplicateCheck     $= "") $Pref::Server::Stargate::DuplicateCheck = true;
    if($Pref::Server::Stargate::EnvironMode        $= "") $Pref::Server::Stargate::EnvironMode = false;

	//Always set this to false so we don't accidently reset the prefs
	$Stargate::RestoreDefaultPrefs = false;
}

function ApplyDefaultPrefValues()
{
	echo("[" @ getWord(getDateTime(), 1) @ "] [Stargate] Applying default pref values");
	messageAll('', "\c6(\c5Stargate\c6) \c6Prefs reset to default values.");

    $Pref::Server::Stargate::MaxHiddenAddresses = 816;
    $Pref::Server::Stargate::RNGSeed = 1563980479;
    $Pref::Server::Stargate::ChatBypass = false;
    $Pref::Server::Stargate::ChatBypassRank = "2";
    $Pref::Server::Stargate::BypassBorder = false;
    $Pref::Server::Stargate::BypassBorderRank = "2";
    $Pref::Server::Stargate::WhatsMyPlanetCmd = false;
    $Pref::Server::Stargate::WhatsMyPlanetCmdRank = 2;
    $Pref::Server::Stargate::DuplicateCheck = true;
    $Pref::Server::Stargate::EnvironMode = false;

	//Always set this to false so we don't accidently reset the prefs
	$Stargate::RestoreDefaultPrefs = false;
}

//Erases outdated prefs from the config file
function DeleteOutdatedPrefs()
{
	//Step 1: Copy all current prefs
    %maxHA = $Pref::Server::Stargate::MaxHiddenAddresses;
    %RNGseed = $Pref::Server::Stargate::RNGSeed;
    %chatBypass = $Pref::Server::Stargate::ChatBypass;
    %chatBypassRank = $Pref::Server::Stargate::ChatBypassRank;
    %bypassBorder = $Pref::Server::Stargate::BypassBorder;
    %bypassBorderRank = $Pref::Server::Stargate::BypassBorderRank;
    %planetcmd = $Pref::Server::Stargate::WhatsMyPlanetCmd;
    %planetcmdrank = $Pref::Server::Stargate::WhatsMyPlanetCmdRank;
    %dupcheck = $Pref::Server::Stargate::DuplicateCheck;
    %environmode = $Pref::Server::Stargate::EnvironMode;

	//Step 2: Delete everything
	deleteVariables("$Pref::Server::Stargate::*");

	//Step 3: Set current prefs again
    $Pref::Server::Stargate::MaxHiddenAddresses = %maxHA;
    $Pref::Server::Stargate::RNGSeed = %RNGseed;
    $Pref::Server::Stargate::ChatBypass = %chatBypass;
    $Pref::Server::Stargate::ChatBypassRank = %chatBypassRank;
    $Pref::Server::Stargate::BypassBorder = %bypassBorder;
    $Pref::Server::Stargate::BypassBorderRank = %bypassBorderRank;
    $Pref::Server::Stargate::WhatsMyPlanetCmd = %planetcmd;
    $Pref::Server::Stargate::WhatsMyPlanetCmdRank = %planetcmdrank;
    $Pref::Server::Stargate::DuplicateCheck = %dupcheck;
    $Pref::Server::Stargate::EnvironMode = %environmode;
}